<?php
/**
 * 用户相关接口
 * 接受数据格式:POST
 * 返回数据格式:JSON
 * 
 * @author Wayne(wayneyu@flaringtech.com)
 * 
 */
defined('BASEPATH') OR exit('No direct script access allowed');
class Circle extends Jiuchi_Controller {
	private $returnUrl;
	private $model;

    function __construct() {
        parent::__construct();
		$this->load->model('user_model');
		$this->load->model('circle_model');
		$this->load->model('circle_category_model');
		$this->load->model('circle_user_model');
		$this->load->model('circle_admin_model');
		$this->load->model('circle_applicant_model');
		$this->load->model('topic_model');
		$this->load->model('user_dynamic_model');
    }
    
    /**
     * 获取用户已加入圈子列表接口
     *
     * 页面	:圈子页面
     * URL	:/message/getusercirclelist
     * 备注	:
     * 验证	:是
     * 接受参数:PageSize, CurPage
     * 返回参数:[{Id,Status,Title,Msg,CreatedAt},{},...]
     */
    public function getusercirclelist(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 2 || !isset($input['PageSize']) || !isset($input['CurPage'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}
    	 
    	!empty($input['CurPage'])
    	&& $input['CurPage'] = $this->varfilter->checkInt($input['CurPage']);
    	if(empty($input['CurPage'])){
    		$input['CurPage'] = 1;
    	}
    	$input['CurPage'] -= 1;
    	!empty($input['PageSize'])
    	&& $input['PageSize'] = $this->varfilter->checkInt($input['PageSize']);
    	if(empty($input['PageSize'])){
    		$this->apioutput->apiResponseError('参数异常');
    	}
    	 
    	$data = $this->circle_user_model->getUserConcernedCircles($this->mytoken->userdata('uid'), $input['PageSize'], $input['PageSize']*$input['CurPage']);
		foreach ($data as $key => $val){
			$cInfo  = $this->circle_model->getInfo($val['circle_id']);
			if($val['is_joined']){
				$joinAct = '<div class="rep-join fr" onclick="btnQuit(this)">退出圈子</div>';
			} else if($this->circle_applicant_model->checkIsApplying($this->mytoken->userdata('uid'), $val['circle_id'])){
				$joinAct = '<div class="rep-join fr" onclick="btnWaitCheck(this)">待审核</div>';
			}else{
				$joinAct = '<div class="rep-join active fr" onclick="btnJoin(this)">申请加入</div>';
			}

			$results[] = array(
					'CircleName'	=>	$cInfo['name'],
					'CircleImg'		=>	'<img width="80px" src="'.(isset($cInfo['circle_img'])?$cInfo['circle_img']:'').'" class="img-cir">',
					'CircleId'		=>	$val['circle_id'],
					'CircleDesc'	=>	$cInfo['description'],
					'MemberCount'	=>	$cInfo['user_number'],
					'TopicCount'	=>	$cInfo['topic_number'],
					'Action'		=>	'<div class="rep-concern fr" onclick="btnUnConcern(this)">取消关注</div>'.$joinAct
			);
		}
    	 
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
    
    public function getallcirclelist(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 2 || !isset($input['PageSize']) || !isset($input['CurPage'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}
    
    	!empty($input['CurPage'])
    	&& $input['CurPage'] = $this->varfilter->checkInt($input['CurPage']);
    	if(empty($input['CurPage'])){
    		$input['CurPage'] = 1;
    	}
    	$input['CurPage'] -= 1;
    	!empty($input['PageSize'])
    	&& $input['PageSize'] = $this->varfilter->checkInt($input['PageSize']);
    	if(empty($input['PageSize'])){
    		$this->apioutput->apiResponseError('参数异常');
    	}
    	$cateId = $this->input->get('CategoryId');
    	$keyword = $this->input->get('Keyword');
    
    	$cond = array();
		if($cateId){
			$cond['circle_category_id'] = $cateId;
		}
		$like = array();
		if($keyword){
			$like['name']	= $keyword;
		}
		$data = $this->circle_model->getAllCircles($input['PageSize'], $input['PageSize']*$input['CurPage'], $cond, $like);
		$circleArr = array();
		foreach ($data as $cInfo){
			if(!$this->circle_user_model->checkIsConcerned($this->session->userdata('uid'), $cInfo['id'])){
				$conAct = '<div class="rep-concern active fr" onclick="btnConcern(this)">关注圈子</div>';
				$joinAct = '<div class="rep-join active fr" onclick="btnJoin(this)">申请加入</div>';
			} else{
				$conAct = '<div class="rep-concern fr" onclick="btnUnConcern(this)">取消关注</div>';
				if($this->circle_user_model->checkIsMember($this->session->userdata('uid'), $cInfo['id'])){
					$joinAct = '<div class="rep-join fr" onclick="btnQuit(this)">退出圈子</div>';
				} else if($this->circle_applicant_model->checkIsApplying($this->session->userdata('uid'), $cInfo['id'])){
					$joinAct = '<div class="rep-join fr" onclick="btnWaitCheck(this)">待审核</div>';
				}else{
					$joinAct = '<div class="rep-join active fr" onclick="btnJoin(this)">申请加入</div>';
				}
			}
			$results[] = array(
					'CircleName'	=>	$cInfo['name'],
					'CircleImg'		=>	'<img width="80px" src="'.(isset($cInfo['circle_img'])?$cInfo['circle_img']:'').'" class="img-cir">',
					'CircleId'		=>	$cInfo['id'],
					'CircleDesc'	=>	$cInfo['description'],
					'MemberCount'	=>	$cInfo['user_number'],
					'TopicCount'	=>	$cInfo['topic_number'],
					'Action'		=>	$conAct.$joinAct
			);
		}
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
    
    public function getcircledetail(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 1 || !isset($input['Id'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}
    	
    	$cInfo = $this->circle_model->getInfo($input['Id']);
    	$results = array(
    			'CircleName'	=>	$cInfo['name'],
    			'CircleImg'		=>	site_url($cInfo['circle_img']),
    			'CircleDesc'	=>	$cInfo['description'],
    			'MemberCount'	=>	$cInfo['user_number'],
    			'TopicCount'	=>	$cInfo['topic_number'],
    	);
    	
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
    
    public function gettopicslist(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 3 || !isset($input['PageSize']) || !isset($input['CurPage']) || !isset($input['Id'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}

    	!empty($input['Id'])
    	&& $input['Id'] = $this->varfilter->checkInt($input['Id']);
    	
    	!empty($input['CurPage'])
    	&& $input['CurPage'] = $this->varfilter->checkInt($input['CurPage']);
    	if(empty($input['CurPage'])){
    		$input['CurPage'] = 1;
    	}
    	$input['CurPage'] -= 1;
    	!empty($input['PageSize'])
    	&& $input['PageSize'] = $this->varfilter->checkInt($input['PageSize']);
    	if(empty($input['PageSize'])){
    		$this->apioutput->apiResponseError('参数异常');
    	}
    	
    	$data = $this->topic_model->getCircleTopics($input['Id'], $input['PageSize'], $input['PageSize']*$input['CurPage']);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['user_id']);
			$results[$key] = array(
					'Avatar'		=>	site_url($uInfo['headportrait']),
					'Name'			=>	isset($uInfo['nickname'])?$uInfo['nickname']:'',
					'Title'			=>	$val['title'],
					'Content'		=>	$val['content'],
					'Commentsnum'	=>	$val['comments_number']
			);
		}
    	 
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
    
    public function getmemberslist(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 3 || !isset($input['PageSize']) || !isset($input['CurPage']) || !isset($input['Id'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}

    	!empty($input['Id'])
    	&& $input['Id'] = $this->varfilter->checkInt($input['Id']);
    	
    	!empty($input['CurPage'])
    	&& $input['CurPage'] = $this->varfilter->checkInt($input['CurPage']);
    	if(empty($input['CurPage'])){
    		$input['CurPage'] = 1;
    	}
    	$input['CurPage'] -= 1;
    	!empty($input['PageSize'])
    	&& $input['PageSize'] = $this->varfilter->checkInt($input['PageSize']);
    	if(empty($input['PageSize'])){
    		$this->apioutput->apiResponseError('参数异常');
    	}
    	
    	$data = $this->circle_user_model->getCircleMembers($input['Id'], $input['PageSize'], $input['PageSize']*$input['CurPage']);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['user_id']);
			$results[$key]['Name'] = isset($uInfo['nickname'])?$uInfo['nickname']:'';
			$results[$key]['Avatar'] = site_url($uInfo['headportrait']);
		}
    	 
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
}